package life.kenny.community.controller;

import life.kenny.community.dto.GiteeUser;
import life.kenny.community.dto.PagenationDto;
import life.kenny.community.model.User;
import life.kenny.community.service.NotificationService;
import life.kenny.community.service.QuestionService;
import life.kenny.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Jay
 * @date 2021/10/28 3:37 下午
 */
@Controller
public class ProfileController {

    @Autowired
    private UserService userService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/profile/{action}")
    public String profile(HttpServletRequest request,
                          @PathVariable("action") String action,
                          Model model,
                          @RequestParam(name = "page",defaultValue = "1")Integer page,
                          @RequestParam(name="size",defaultValue = "5")Integer size) {
        Cookie[] cookies = request.getCookies();
        HttpSession session = request.getSession();
//        GiteeUser user1 = (GiteeUser) session.getAttribute("user");
        User user = (User) request.getSession().getAttribute("user");
        String accountId = "";
        if (user == null) {
            return "redirect:/";
        }
//        accountId = String.valueOf(user.getId());
        if("questions".equals(action)){
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的问题");
            PagenationDto pagination = questionService.listByUserId(page, size, Long.valueOf(user.getAccountId()));
            model.addAttribute("pagination", pagination);
        }
        if ("replies".equals(action)) {
            PagenationDto pagination = notificationService.list(Long.valueOf(user.getAccountId()), page, size);
            model.addAttribute("section", "replies");
            model.addAttribute("sectionName", "最新回复");
            model.addAttribute("pagination", pagination);
        }
        return "profile";
    }
}
